﻿using System.Windows;
using SmartPeople.Framework.SecuritySpace;

namespace SecuritySpace.WPF.Demo
{
    /// <summary>
    /// Interaction logic for MainMenu.xaml
    /// </summary>
    public partial class MainMenu : Window
    {
        private ISecuritySpaceProviderFactory securitySpaceProviderFactory = new XPOProviderFactory();
        private IToken currentUserToken;
        private ISecuritySpace currentSecuritySpace;

        public MainMenu(IToken userToken, ISecuritySpace securitySpace)
        {
            currentUserToken = userToken;
            currentSecuritySpace = securitySpace;
            InitializeComponent();
            IntializeSecurityDisplay();
        }


        public void IntializeSecurityDisplay()
        {
            ISecurityUser user = currentSecuritySpace.Users.GetUser(currentUserToken, true);

            //if(currentSecuritySpace.IsUserGranted(currentUserToken,"/",,))
            //    lblInfo.Content = "You have access to role: " + s + "\n";


            if (user.UserName != null)
                Title = "Welcome " + user.UserName;
            string[] currentUserRoles = currentSecuritySpace.Roles.GetRolesForUser(user.UserName);
            foreach (string s in currentUserRoles)
            {
                lblInfo.Content = "You have access to role: " + s + "\n";
            }

            if (currentUserRoles.Length == 1)
            {
                ISecurityRole role = currentSecuritySpace.Roles.GetRole(currentUserRoles[0]);
                ISecuritySecuredDocument document = currentSecuritySpace.SecuredDocuments.GetDocument("/");
                if (currentSecuritySpace.SecurityPermissions.IsGranted(role.GID, document.GID) == true)
                {
                    lblInfo2.Content = "You have access to document: " + document.Name;
                }

                ISecuritySecuredTypeActions actions =
                    currentSecuritySpace.SecuredDocuments.GetDocument("/").SecuredType.Actions;


                //currentSecuritySpace.
                //      Guid actionGuid = securitySpace.SecuredDocuments.GetDocument(commonPath).GetActionID(TestActions.Create);
            }
        }


        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown(0);
        }
    }
}